﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace XrmLibrary.MsCrm4Adapter.Extensions
{
    public static class EntityReferenceExtensions
    {
        public static Microsoft.Crm.Sdk.Moniker ToCrm4Moniker(this Microsoft.Xrm.Sdk.EntityReference crm2011EntityReference)
        {
            Microsoft.Crm.Sdk.Moniker moniker =
                new Microsoft.Crm.Sdk.Moniker()
                {
                    Id = crm2011EntityReference.Id,
                    Name = crm2011EntityReference.LogicalName
                };

            return moniker;
        }

        public static Microsoft.Crm.SdkTypeProxy.TargetOwned ToCrm4TargetOwned(this Microsoft.Xrm.Sdk.EntityReference crm2011EntityReference)
        {
            Microsoft.Crm.SdkTypeProxy.TargetOwnedDynamic target =
                new Microsoft.Crm.SdkTypeProxy.TargetOwnedDynamic()
                {
                    EntityId = crm2011EntityReference.Id,
                    EntityName = crm2011EntityReference.LogicalName
                };

            return target;
        }

        public static Microsoft.Crm.SdkTypeProxy.TargetRollup ToCrm4TargetRollup(this Microsoft.Xrm.Sdk.EntityReference crm2011EntityReference, Microsoft.Crm.Sdk.Messages.RollupRequest crm2011TargetRollup)
        {
            Microsoft.Crm.SdkTypeProxy.TargetRollupDynamic target =
                new Microsoft.Crm.SdkTypeProxy.TargetRollupDynamic()
                {
                    EntityId = crm2011EntityReference.Id,
                    EntityName = crm2011EntityReference.LogicalName,
                    Query = crm2011TargetRollup.Query.ToCrm4QueryExpression()
                };

            return target;
        }

        public static Microsoft.Crm.SdkTypeProxy.TargetRelated ToCrm4TargetRelated(this Microsoft.Xrm.Sdk.EntityReference[] entityReference)
        {
            if (entityReference.Length == 0)
                throw new ArgumentOutOfRangeException("Microsoft.Xrm.Sdk.EntityReference[] array cannot be empty.");

            Microsoft.Crm.SdkTypeProxy.TargetRelatedDynamic target =
                new Microsoft.Crm.SdkTypeProxy.TargetRelatedDynamic()
                {
                    Entity1Id = entityReference[0].Id,
                    Entity1Name = entityReference[0].LogicalName,
                    Entity2Id = entityReference[1].Id,
                    Entity2Name = entityReference[1].LogicalName
                };

            return target;
        }

        public static Microsoft.Crm.Sdk.SecurityPrincipal ToCrm4SecurityPrincipal(this Microsoft.Xrm.Sdk.EntityReference entityReference)
        {
            Microsoft.Crm.Sdk.SecurityPrincipal securityPrincipal = new Microsoft.Crm.Sdk.SecurityPrincipal();

            securityPrincipal.PrincipalId = entityReference.Id;
            
            if (entityReference.LogicalName == "systemuser")
                securityPrincipal.Type = Microsoft.Crm.Sdk.SecurityPrincipalType.User;
            else
                securityPrincipal.Type = Microsoft.Crm.Sdk.SecurityPrincipalType.Team;

            return securityPrincipal;
        }

        public static Microsoft.Crm.SdkTypeProxy.TargetRemoveParent ToCrm4TargetRemoveParent(this Microsoft.Xrm.Sdk.EntityReference entityReference)
        {
            Microsoft.Crm.SdkTypeProxy.TargetRemoveParentDynamic targetRemoveParent =
                new Microsoft.Crm.SdkTypeProxy.TargetRemoveParentDynamic()
                {
                    EntityId = entityReference.Id,
                    EntityName = entityReference.LogicalName
                };

            return targetRemoveParent;
        }
    }
}
